package pt.ua.helpdesk.client;

import pt.ua.helpdesk.client.LoginRegistar.RegistarAsyncCallback;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class UserAdd extends Composite {

	public UserAdd(String usrtype) {
		final String user_type = usrtype ;
		
		final VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		final HorizontalPanel horizontalPanel_8 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_8);

		final Label lblNome = new Label("Nome:");
		horizontalPanel_8.add(lblNome);
		lblNome.setStyleName("label");

		final TextBox txtNome = new TextBox();
		horizontalPanel_8.add(txtNome);
		txtNome.setWidth("250");
		txtNome.setStyleName("textbox");

		final HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_3);
		horizontalPanel_3.setSpacing(2);
		horizontalPanel_3.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		final Label lblUtilizador = new Label("Utilizador:");
		horizontalPanel_3.add(lblUtilizador);
		lblUtilizador.setStyleName("label");

		final TextBox txtUser = new TextBox();
		horizontalPanel_3.add(txtUser);
		txtUser.setWidth("250");
		txtUser.setStyleName("textbox");

		final HorizontalPanel horizontalPanel_4 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_4);
		horizontalPanel_4.setSpacing(2);
		horizontalPanel_4.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		final Label lblPass1 = new Label("Palavra-Chave:");
		horizontalPanel_4.add(lblPass1);
		lblPass1.setStyleName("label");

		final PasswordTextBox txtPass1 = new PasswordTextBox();
		horizontalPanel_4.add(txtPass1);
		txtPass1.setWidth("250");
		txtPass1.setStyleName("textbox");

		final HorizontalPanel horizontalPanel_5 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_5);
		horizontalPanel_5.setSpacing(2);

		final Label lblPass2 = new Label("Confirmar Palavra-Chave:");
		horizontalPanel_5.add(lblPass2);
		lblPass2.setStyleName("label");

		final PasswordTextBox txtPass2 = new PasswordTextBox();
		horizontalPanel_5.add(txtPass2);
		txtPass2.setWidth("250");
		txtPass2.setStyleName("textbox");

		final HorizontalPanel horizontalPanel_6 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_6);
		horizontalPanel_6.setSpacing(2);

		final Label lblEmail = new Label("E-mail:");
		horizontalPanel_6.add(lblEmail);
		lblEmail.setStyleName("label");

		final TextBox txtEmail = new TextBox();
		horizontalPanel_6.add(txtEmail);
		txtEmail.setWidth("250");
		txtEmail.setStyleName("textbox");

		final HorizontalPanel horizontalPanel_7 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_7);
		horizontalPanel_7.setSpacing(4);
	
		final HTML htmlAlerta = new HTML("New <i>HTML</i> panel");
		
		final Button btnRegistar = new Button();
		horizontalPanel_7.add(btnRegistar);
		btnRegistar.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				
				boolean alerta = false;
				int c = 0;
				
				if(txtNome.getText().equals(""))
				{
					if(c == 0)
					{
						htmlAlerta.setHTML("<u>ALERTA:</u><br>");
						c++;
					}
					alerta = true;
					htmlAlerta.setHTML(htmlAlerta.getHTML() + "» tem de introduzir o seu nome!<br>");
				}
				
				if(txtUser.getText().equals(""))
				{
					if(c == 0)
					{
						htmlAlerta.setHTML("<u>ALERTA:</u><br>");
						c++;
					}
					alerta = true;
					htmlAlerta.setHTML(htmlAlerta.getHTML() + "» tem de introduzir o nome do Utilizador!<br>");
				}
				
				if(txtPass1.getText().equals("") || txtPass2.getText().equals("") )
				{
					if(c == 0)
					{
						htmlAlerta.setHTML("<u>ALERTA:</u><br>");
						c++;
					}
					alerta = true;
					htmlAlerta.setHTML(htmlAlerta.getHTML() + "» tem de introduzir as palavras-chave!<br>");
				}
				else if (!txtPass1.getText().equals(txtPass2.getText()))
				{
					if(c == 0)
					{
						htmlAlerta.setHTML("<u>ALERTA:</u><br>");
						c++;
					}
					alerta = true;
					htmlAlerta.setHTML(htmlAlerta.getHTML() + "» as palavras-chave não são iguais!<br>");
				}
				
				if(txtEmail.getText().equals(""))
				{
					if(c == 0)
					{
						htmlAlerta.setHTML("<u>ALERTA:</u><br>");
						c++;
					}
					alerta = true;
					htmlAlerta.setHTML(htmlAlerta.getHTML() + "» tem de introduzir o E-mail! <br>");
				}
				else
					if(!txtEmail.getText().matches(".+@.+\\.[a-z]+"))
					{
						if(c == 0)
						{
							htmlAlerta.setHTML("<u>ALERTA:</u><br>");
							c++;
						}
						alerta = true;
						htmlAlerta.setHTML(htmlAlerta.getHTML() + "» o e-mail não é válido! <br>");
					}
				
				if(!alerta)
				{
					htmlAlerta.setHTML("a processar dados...");
					ServiceAdministration.Util.getInstance().addUser(txtUser.getText(),txtPass1.getText(), txtEmail.getText(), txtNome.getText(), user_type, new RegistarAsyncCallback(htmlAlerta));
				}
			}
		});
		btnRegistar.setStyleName("button");
		btnRegistar.setText("REGISTAR");

		final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_2);
		horizontalPanel_2.setSpacing(2);


		horizontalPanel_2.add(htmlAlerta);
		htmlAlerta.setText("");
		htmlAlerta.setStyleName("erro");
	}
	
	static class RegistarAsyncCallback implements AsyncCallback {
		HTML html;
		  
		  /**
		   * Class constructor
		   * @param html Widget that can contain arbitrary HTML
		   */
		  public RegistarAsyncCallback(HTML html) {
			  this.html= html;
		  }
		  
		  /**
		   * Success handler
		   */
		  public void onSuccess(Object object) {
			  String alerta = (String)object;
			  String message = "";
			  
			  if (alerta.indexOf('9') != -1)
	    		  message += "» Ocorreu um Erro inesperado!<br>";
			  else
			  {
				  if (!alerta.equals(""))
					  message += "<u>ERRO:</u><br>";
					  
		    	  if(alerta.indexOf('1') != -1)
		    		  message += "» Esse E-mail já Existe!<br>";
		    	  if (alerta.indexOf('2') != -1)
		    		  message += "» Esse Assistente já Existe!<br>";
		    	  
		    	  if (alerta.equals(""))
		    		  message = "Registo efectuado com sucesso!";
			  }

  		  DOM.setInnerHTML(html.getElement(), message);
  		  
		  }
		  
		  /**
		   * Failure handler
		   */
		  public void onFailure(Throwable throwable) {
			  System.out.println("UserAdd :: FALHOU!");
		  }
	}

}
